import { _decorator, Component, Node, resources, NodeEventType } from 'cc';
import * as ccenv from 'cc/env'
import View from './view/View';
import FragmentManager, { Config, Layer } from './FragmentManager';

const isPC = ccenv.MAC || ccenv.WINDOWS || ccenv.LINUX;
const EventClick = isPC ? NodeEventType.MOUSE_DOWN : NodeEventType.TOUCH_START;
const { ccclass, property } = _decorator;

class Poker extends View {
    layer(): Layer {
        return Layer.Game;
    }
    create(root: Node): { prefab: string; bundle?: string; } | Node {
        return {
            prefab: 'prefab/Poker',
            bundle: 'bundle'
        }
    }

    onAdd(manager: FragmentManager, view: View, node: Node): void {
        console.log('onAdd >>  ')
        node.on(EventClick, () => {
            // node.removeFromFragment();
            manager.toast('asdasdasdad', false)
        })
    }

    // onAddBefore(node: Node, root: Node, params: {}): Promise<void> {

    //     console.log('onAddBefore >>  ')

    //     return super.onAddBefore(node, root, params)
    // }
}

@ccclass('Gui')
export class Gui extends Component {
    async start() {
        const config = new Config();
        config.defauleBundle = resources;
        const manager = new FragmentManager(this.node, config);
        await manager.open(Poker)
        await manager.open(Poker)

        // manager.open(Poker).then((result) => {
        //     console.log('success', result)
        // }).catch((err) => {
        //     console.log('failed', err)
        // })

        // manager.open(Poker).then((result) => {
        //     console.log('success', result)
        // }).catch((err) => {
        //     console.log('failed', err)
        // })
    }

    update(deltaTime: number) {

    }
}


